package com.aspose.cells;

import java.util.ArrayList;
import java.util.Iterator;

public class PivotFieldCollection {
   ArrayList a;
   PivotTable b;
   int c;
   int d;

   PivotFieldCollection(PivotTable pivotTable, int fieldType) {
      this.b = pivotTable;
      this.a = new ArrayList();
      this.c = fieldType;
   }

   PivotFieldCollection(PivotTable pivotTable, zauk pivotCache) {
      this.b = pivotTable;
      this.a = new ArrayList();
      this.c = 0;

      for(int var3 = 0; var3 < pivotCache.i; ++var3) {
         PivotField var4 = new PivotField(this);
         if (var3 < pivotCache.p.size()) {
            var4.h = (zbqs)pivotCache.p.get(var3);
         }

         var4.a(var3);
         com.aspose.cells.b.a.a.ze.a(this.a, (Object)var4);
      }

   }

   PivotFieldCollection(PivotTable pivotTable, zauk pivotCache, int count) {
      this.b = pivotTable;
      this.a = new ArrayList();
      this.c = 0;

      for(int var4 = 0; var4 < count; ++var4) {
         PivotField var5 = new PivotField(this);
         var5.h = new zbqs(pivotCache);
         var5.a(var4);
         com.aspose.cells.b.a.a.ze.a(this.a, (Object)var5);
      }

   }

   void a(zbqs var1) {
      this.b.al = null;
      PivotField var2 = new PivotField(this);
      var2.b.a = 0;
      var2.c.a = 13328;
      var2.h = var1;
      var2.a(this.getCount());
      com.aspose.cells.b.a.a.ze.a(this.a, (Object)var2);
   }

   public int getType() {
      return this.c;
   }

   public int getCount() {
      return this.a.size();
   }

   public PivotField get(int index) {
      return (PivotField)this.a.get(index);
   }

   public PivotField get(String name) {
      Iterator var2 = this.a.iterator();

      PivotField var3;
      do {
         if (!var2.hasNext()) {
            return null;
         }

         var3 = (PivotField)var2.next();
      } while(var3.getName() == null || !var3.getName().toUpperCase().equals(name.toUpperCase()));

      return var3;
   }

   void a(int var1) {
      this.b.al = null;
      this.a.remove(var1);
   }

   boolean a() {
      for(int var1 = 0; var1 < this.a.size() - 1; ++var1) {
         PivotField var2 = (PivotField)this.a.get(var1);
         if (var2.getInsertBlankRow() && !var2.i()) {
            return true;
         }
      }

      return false;
   }

   int b() {
      int var1 = 0;

      for(int var2 = 0; var2 < this.a.size(); ++var2) {
         PivotField var3 = (PivotField)this.a.get(var2);
         if (var3.u) {
            ++var1;
         }
      }

      return var1;
   }

   public int addByBaseIndex(int baseFieldIndex) {
      return this.add(this.b.getBaseFields().get(baseFieldIndex));
   }

   private void b(PivotField var1, int var2) {
      if (!var1.i()) {
         if (!var1.isCalculatedField()) {
            zbqs var3 = var1.h;
            if (var3 != null) {
               if (var3.c == null) {
                  if (var3.t()) {
                     throw new CellsException(12, "This pivot field has more unique items than can be used in a pivot table.");
                  }

                  if (var3.a().n != null) {
                     var3.a().n.a(var3);
                  }

                  if (var3.t()) {
                     throw new CellsException(12, "This pivot field has more unique items than can be used in a pivot table.");
                  }

                  if (var3.c != null && var3.c.size() > var2) {
                     throw new CellsException(12, "This pivot field has more unique items than can be used in a pivot table.");
                  }
               } else if (var3.c.size() > var2) {
                  throw new CellsException(12, "This pivot field has more unique items than can be used in a pivot table.");
               }
            }

         }
      }
   }

   public int add(PivotField pivotField) {
      this.b.al = null;
      if (pivotField == null) {
         throw new CellsException(12, "You could not null to the specific type PivotFields.");
      } else if (pivotField.a.c != 0) {
         throw new CellsException(12, "This pivot field has been dragged to specific area.");
      } else {
         int var2 = pivotField.m;
         if (this.c == var2) {
            return this.d(pivotField);
         } else {
            this.b.s = false;
            switch(this.c) {
            case 0:
               throw new CellsException(12, "You could not add field to base fields");
            case 1:
               if (!pivotField.getDragToRow()) {
                  throw new CellsException(12, "This pivot field can't be dragged to the row position");
               }

               if (!this.b.a.b.getWorkbook().k()) {
                  this.b(pivotField, 32767);
               }

               if (var2 == 0) {
                  pivotField.initPivotItems();
               } else {
                  this.e(pivotField);
                  pivotField.initPivotItems();
               }

               pivotField.m = this.c;
               com.aspose.cells.b.a.a.ze.a(this.a, (Object)pivotField);
               int var5 = this.a.size() - 1;
               this.b(var5);
               return var5;
            case 2:
               if (!pivotField.getDragToColumn()) {
                  throw new CellsException(12, "This pivot field can't be dragged to the column position");
               }

               boolean var4 = false;
               short var6;
               if (!this.b.a.b.getWorkbook().k()) {
                  var6 = 255;
               } else {
                  var6 = 16383;
               }

               this.b(pivotField, var6);
               if (var2 == 0) {
                  pivotField.initPivotItems();
               } else {
                  this.e(pivotField);
                  pivotField.initPivotItems();
               }

               pivotField.m = this.c;
               com.aspose.cells.b.a.a.ze.a(this.a, (Object)pivotField);
               return this.getCount() - 1;
            case 3:
            case 5:
            case 6:
            case 7:
            default:
               return -1;
            case 4:
               if (!pivotField.getDragToPage()) {
                  throw new CellsException(12, "This pivot field can't be dragged to the page position");
               } else {
                  if (pivotField.l != null) {
                     throw new CellsException(12, "You could not add data field to the page fields");
                  }

                  this.b(pivotField, 32767);
                  if (var2 == 0) {
                     pivotField.initPivotItems();
                  } else {
                     this.e(pivotField);
                     pivotField.initPivotItems();
                  }

                  if (this.getCount() + 1 > this.b.i) {
                     this.b.i += 2;
                  } else if (this.getCount() + 1 == this.b.i) {
                     ++this.b.i;
                  }

                  pivotField.j = new zbrl(pivotField);
                  pivotField.m = this.c;
                  com.aspose.cells.b.a.a.ze.a(this.a, (Object)pivotField);
                  return this.getCount() - 1;
               }
            case 8:
               pivotField.a(true);
               PivotField var3 = new PivotField(this, pivotField);
               com.aspose.cells.b.a.a.ze.a(this.a, (Object)var3);
               var3.a(this.a.size() - 1);
               this.a(true, this.getCount() - 1);
               return this.getCount() - 1;
            }
         }
      }
   }

   int a(PivotField var1, int var2) {
      this.b.al = null;
      if (var1 == null) {
         throw new CellsException(12, "You could not null to the specific type PivotFields.");
      } else if (var1.a.c != 0) {
         throw new CellsException(12, "This pivot field has been dragged to specific area.");
      } else {
         int var3 = var1.m;
         if (this.c == var3) {
            return this.d(var1);
         } else {
            this.b.s = false;
            switch(this.c) {
            case 0:
               throw new CellsException(12, "You could not add field to base fields");
            case 1:
               if (!var1.getDragToRow()) {
                  throw new CellsException(12, "This pivot field can't be dragged to the row position");
               }

               this.b(var1, 32767);
               if (var3 == 0) {
                  var1.initPivotItems();
               } else {
                  this.e(var1);
               }

               var1.m = this.c;
               this.a.add(var2, var1);
               this.b(var2);
               return var2;
            case 2:
               if (!var1.getDragToColumn()) {
                  throw new CellsException(12, "This pivot field can't be dragged to the column position");
               }

               boolean var5 = false;
               short var6;
               if (!this.b.a.b.getWorkbook().k()) {
                  var6 = 255;
               } else {
                  var6 = 16383;
               }

               this.b(var1, var6);
               if (var3 == 0) {
                  var1.initPivotItems();
               } else {
                  this.e(var1);
               }

               var1.m = this.c;
               this.a.add(var2, var1);
               return this.getCount() - 1;
            case 3:
            case 5:
            case 6:
            case 7:
            default:
               return -1;
            case 4:
               if (!var1.getDragToPage()) {
                  throw new CellsException(12, "This pivot field can't be dragged to the page position");
               } else {
                  if (var1.l != null) {
                     throw new CellsException(12, "You could not add data field to the page fields");
                  }

                  this.b(var1, 32767);
                  if (var3 == 0) {
                     var1.initPivotItems();
                  } else {
                     this.e(var1);
                  }

                  if (this.getCount() + 1 > this.b.i) {
                     this.b.i += 2;
                  } else if (this.getCount() + 1 == this.b.i) {
                     ++this.b.i;
                  }

                  var1.j = new zbrl(var1);
                  var1.m = this.c;
                  this.a.add(var2, var1);
                  return this.getCount() - 1;
               }
            case 8:
               if (var1.l != null) {
                  throw new CellsException(12, "You could not add data field to the data fields");
               } else {
                  var1.a(true);
                  PivotField var4 = new PivotField(this, var1);
                  com.aspose.cells.b.a.a.ze.a(this.a, (Object)var4);
                  var4.a(this.a.size() - 1);
                  this.a(true, this.getCount() - 1);
                  return this.getCount() - 1;
               }
            }
         }
      }
   }

   void c() {
      int var1 = this.getCount();
      if (var1 > 1) {
         for(int var2 = 0; var2 < var1; ++var2) {
            this.b(var2);
         }

      }
   }

   private void b(int var1) {
      if (this.getCount() > 1) {
         switch(this.b.getAutoFormatType()) {
         case 2:
         case 3:
         case 4:
         case 9:
         case 10:
         case 12:
         case 13:
         case 14:
         case 15:
         case 16:
         case 18:
            if (var1 == 0) {
               this.get(0).setInsertBlankRow(true);
               this.get(1).setInsertBlankRow(false);
            } else {
               this.get(var1).setInsertBlankRow(false);
            }
            break;
         case 5:
         case 6:
         case 7:
         case 8:
         case 11:
            if (var1 == this.getCount() - 1) {
               this.get(var1).setInsertBlankRow(false);
               this.get(var1 - 1).setInsertBlankRow(true);
            } else {
               this.get(var1).setInsertBlankRow(true);
            }
         case 17:
         }

      }
   }

   void a(PivotField var1) {
      this.b.al = null;
      com.aspose.cells.b.a.a.ze.a(this.a, (Object)var1);
   }

   void b(PivotField var1) {
      this.b.al = null;
      if (this.c != 0) {
         this.b.s = false;

         for(int var2 = 0; var2 < this.getCount(); ++var2) {
            if (this.get(var2).getBaseIndex() == var1.getBaseIndex()) {
               this.a(var2);
               --var2;
            }
         }

         if (this.c == 8) {
            var1.a(false);
         } else {
            var1.m = 0;
         }

      }
   }

   public void clear() {
      this.b.al = null;
      int var1;
      if (this.c == 8) {
         if (this.b.f != null && this.getCount() > 1) {
            this.c(this.b.f);
         } else {
            for(var1 = this.getCount() - 1; var1 >= 0; --var1) {
               this.c(this.get(var1));
            }
         }
      } else if (this.c != 1 && this.c != 2) {
         if (this.c == 4) {
            for(var1 = this.getCount() - 1; var1 >= 0; --var1) {
               this.c(this.get(var1));
            }
         }
      } else {
         if (this.b.f != null) {
            this.c(this.b.f);
         }

         for(var1 = this.getCount() - 1; var1 >= 0; --var1) {
            this.c(this.get(var1));
         }
      }

   }

   void c(PivotField var1) {
      this.b.al = null;
      if (this.c != 0) {
         this.b.s = false;
         int var2 = this.b.getDataFields().getCount();

         int var3;
         for(var3 = 0; var3 < this.getCount(); ++var3) {
            if (this.get(var3).getBaseIndex() == var1.getBaseIndex()) {
               this.a(var3);
               --var3;
            }
         }

         this.b.e();
         if (this.c == 8) {
            var1.a(false);
            if (this.b.f != null) {
               if (this.b.f.m == 1) {
                  this.b.getRowFields().c(this.b.f);
                  this.b.g = null;
               } else if (this.b.f.m == 2) {
                  this.b.getColumnFields().c(this.b.f);
                  this.b.h = null;
               }

               this.b.f = null;
            }
         } else {
            if (var1 == this.b.f && var2 > 1) {
               this.b.b.c.a.clear();
               this.b.f = null;

               for(var3 = 0; var3 < this.b.getBaseFields().getCount(); ++var3) {
                  PivotField var4 = this.b.getBaseFields().get(var3);
                  if (var4.h()) {
                     var4.a(false);
                  }
               }
            }

            var1.m = 0;
         }

      }
   }

   private void a(boolean var1, int var2) {
      PivotFieldCollection var3 = this.b.getDataFields();
      PivotField var4 = this.b.f;
      PivotFieldCollection var5 = null;
      int var6;
      if (var4 != null) {
         var6 = this.b.getRowFields().d(var4);
         if (var6 == -1) {
            var5 = this.b.getColumnFields();
         } else {
            var5 = this.b.getRowFields();
         }
      }

      if (var3.getCount() <= 1) {
         if (var4 == null) {
            return;
         }

         var5.c(var4);
         this.b.f = null;
      } else {
         if (var4 == null) {
            var4 = this.b.f = new PivotField(this.b, var3);
            var4.a(65534);
            var5 = this.b.getRowFields();
            com.aspose.cells.b.a.a.ze.a(var5.a, (Object)var4);
            return;
         }

         if (var1) {
            PivotItem var10 = new PivotItem(var4.d);
            var4.d.a(var10);
            var10.setIndex(var2);
            var10.e = var3.get(var2);
         } else {
            var6 = var4.d.getCount();
            var4.d.a(var2);
            if (var2 != var6 - 1) {
               int var7 = 0;
               Iterator var8 = var4.d.b.iterator();

               while(var8.hasNext()) {
                  PivotItem var9 = (PivotItem)var8.next();
                  var9.setIndex(var7++);
               }
            }
         }
      }

   }

   int d(PivotField var1) {
      int var2 = -1;
      Iterator var3 = this.a.iterator();

      PivotField var4;
      do {
         if (!var3.hasNext()) {
            return -1;
         }

         var4 = (PivotField)var3.next();
         ++var2;
         if (var4 == var1) {
            return var2;
         }
      } while(var4.getBaseIndex() != var1.getBaseIndex());

      return var2;
   }

   Object a(String var1) {
      if (var1 == null) {
         return null;
      } else {
         Iterator var2 = this.a.iterator();

         PivotField var3;
         do {
            if (!var2.hasNext()) {
               return null;
            }

            var3 = (PivotField)var2.next();
         } while(!var3.getName().toUpperCase().equals(var1.toUpperCase()));

         return var3;
      }
   }

   void e(PivotField var1) {
      this.b.al = null;
      zbrw var2 = this.b.b;
      switch(var1.m) {
      case 0:
         throw new CellsException(12, "Can't remove the base field from BaseFields.");
      case 1:
         var2.d.b(var1);
         break;
      case 2:
         var2.e.b(var1);
         break;
      case 3:
      case 5:
      case 6:
      case 7:
      default:
         throw new CellsException(12, "Invalid field type");
      case 4:
         var1.j.a = null;
         var1.j = null;
         var2.f.b(var1);
         break;
      case 8:
         var1.a = this.b.getBaseFields();
         var2.c.b(var1);
         var1.k.a(false);
      }

   }
}
